package website.lucaswork.survey.api.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import website.lucaswork.survey.core.dtos.SurveySummaryDTO;
import website.lucaswork.survey.core.entities.Survey;
import website.lucaswork.survey.core.services.SurveyService;

@RestController
@Api(value="survey")
public class SurveyController extends SurveyApiWCI {
	
	@Autowired
	SurveyService surveyService;
	
	@RequestMapping(value="/survey/info/{survey}", method=RequestMethod.GET)
	public SurveySummaryDTO getInfo(
			@PathVariable("survey") Survey survey
			) {
		SurveySummaryDTO ret = surveyService.getSurveyInfo(survey.getId());
		return ret;
	}
	
	@RequestMapping(value="/survey/latest", method=RequestMethod.GET)
	@ApiOperation(value="Get paged latest surveys")
	public Page<SurveySummaryDTO> getLatestSurveys(
			@PageableDefault(size=8, page=0, sort={"createdAt"}, direction=Direction.DESC) Pageable pageable
			) {
		return surveyService.getPageSurveys(pageable);
	}
	
	@RequestMapping(value="/survey/popular", method=RequestMethod.GET)
	@ApiOperation(value="Get paged most popular surveys")
	public Page<SurveySummaryDTO> getPopularSurveys(
			@PageableDefault(size=8, page=0, sort={"doneCount"}, direction=Direction.DESC) Pageable pageable
			) {
		return surveyService.getPageSurveys(pageable);
	}
	
	@RequestMapping(value="/survey/list", method=RequestMethod.GET)
	@ApiOperation(value="Get paged surveys")
	public Page<SurveySummaryDTO> getSurveys(
			@RequestParam(value="keyword", defaultValue="", required=false) String keyword,
			@PageableDefault(size=12, page=0) Pageable pageable
			) {
		return surveyService.getSearchPageSurveys(keyword, pageable);
	}
}
